<?php

namespace app\batch;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Debug;
use app\models\{Goods, GoodsSpec, SystemGoods, SystemGoodsSpec};
use think\Db;
use think\facade\Log;
use app\facade\Redis;

class Rebot extends Command
{
    protected $open = false;
    protected function configure()
    {
        $this->setName('Rebot')->setDescription('测试');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->doCron();
        die();
    }

    protected function doCron()
    {
        ini_set('max_execution_time', 0);
        (new SystemGoods())->chunk(1, function ($lists) {
            foreach ($lists as $list) {
                $frtimes = Redis::get('orderRefund' . $list['id']);
                if ($frtimes) {
                    continue;
                } else {
                    Redis::setex('orderRefund' . $list['id'], 10, 10);
                }
                if ($list['classid'] == 154) {
                    $list['classid'] = 2;
                }
                if ($list['classid'] == 155) {
                    $list['classid'] = 9;
                }
                if ($list['classid'] == 156) {
                    $list['classid'] = 5;
                }
                if ($list['classid'] == 157) {
                    $list['classid'] = 7;
                }
                if ($list['classid'] == 158) {
                    $list['classid'] = 11;
                }
                if ($list['classid'] == 159) {
                    $list['classid'] = 6;
                }
                if ($list['classid'] == 160) {
                    $list['classid'] = 12;
                }
                if ($list['classid'] == 161) {
                    $list['classid'] = 4;
                }
                if ($list['classid'] == 162) {
                    $list['classid'] = 8;
                }
                if ($list['classid'] == 163) {
                    $list['classid'] = 10;
                }
                if ($list['classid'] == 164) {
                    $list['classid'] = 13;
                }
                if ($list['classid'] == 165) {
                    $list['classid'] = 14;
                }
                if ($list['classid'] == 166) {
                    $list['classid'] = 1;
                }
                if ($list['classid'] == 167) {
                    $list['classid'] = 3;
                }
                (new SystemGoods())->where('id', $list['id'])->update(['classid' => $list['classid']]);
                // $spec = (new GoodsSpec())->where('goodid', $list['id'])->find();
                // if (!empty($spec)) {
                //     $goods = [
                //         'classid' => $list['cate'],
                //         'title' => $list['title'],
                //         'images' => $list['images'],
                //         'recomm' => $list['isHot'],
                //         'isnew' => $list['isNew'],
                //         'keywords' => $list['Keywords'],
                //         'descs' => $list['descs'],
                //         'goodsDesc' => $list['goodsDesc']
                //     ];
                //     $goodspec = [
                //         'sales' => $list['sale'],
                //         'goodid' => 0,
                //         'name' => $spec['name'],
                //         'price' => $spec['price'],
                //         'unmit' => ''
                //     ];
                //     Db::startTrans();
                //     try {
                //         $goodid = (new SystemGoods())->insertGetId($goods);
                //         $goodspec['goodid'] = $goodid;
                //         (new SystemGoodsSpec())->allowfield(true)->save($goodspec);
                //         Db::commit();
                //     } catch (\Exception $e) {
                //         dump($e->getMessage());
                //         Db::rollback();
                //     }
                // }
            }
        });
    }
}